/**
 *   类说明
 * 
 *   @creator         xldc	497937995@qq.com  
 *   @create-time     2014年1月15日   下午6:40:18   
 */
package com.huacheng.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huacheng.cms.constant.FolderConstant;
import com.huacheng.cms.dao.ArticleMapper;
import com.huacheng.cms.dao.FolderMapper;
import com.huacheng.cms.model.Article;
import com.huacheng.cms.model.Folder;
import com.huacheng.cms.model.vo.ArticleVo;
import com.huacheng.cms.model.vo.FolderVo;

@Service
public class FolderService {

	@Autowired
	private FolderMapper folderM;

	@Autowired
	private ArticleMapper articleM;

	public boolean addOne(Folder folder) {
		int selects = -1;
		Folder f = new Folder();
		f.setName(folder.getName());
		selects = folderM.selectBySelectiveCount(f);
		if (selects == 0) {
			if (folder.getParentid() == null) {
				folder.setParentid(Long.valueOf(0));
			} else {
				if (folder.getParentid() != 0) {
					Folder fp = folderM.selectByPrimaryKey(folder.getParentid());
					fp.setChildrencount(fp.getChildrencount() + 1);
					folderM.updateByPrimaryKeySelective(fp);
				}
			}
			folder.setChildrencount(0);
			folder.setArticlecount(0);
			if (folder.getSort() == null){
				folder.setSort(0);
			}
			folder.setStatus(FolderConstant.Status.display.toString());
			folderM.insertSelective(folder);
		} else {
			return false;
		}
		return true;

	}

	public Folder getOneByKey(long folderid) {
		return folderM.selectByPrimaryKey(folderid);
	}

	public void hidenByKey(long folderid) {
		Folder folder = folderM.selectByPrimaryKey(folderid);
		// 无此项直接跳出
		if (folder == null) {
			return;
		}
		// 接着隐藏相关孩子节点
		Folder f = new Folder();
		f.setParentid(folderid);
		List<FolderVo> children = folderM.selectBySelective(f, 0, 0);
		for (FolderVo fv : children) {
			// 再隐藏相关文章
			Article art = new Article();
			art.setFolderid(fv.getFolderid());
			List<ArticleVo> art_list = articleM.selectBySelective(art, Long.valueOf(0), Long.valueOf(0));
			for (ArticleVo a : art_list) {
				a.setStatus(FolderConstant.Status.hidden.toString());
				articleM.updateByPrimaryKeySelective(a);
			}
			hidenByKey(fv.getFolderid());
		}
		folder.setStatus(FolderConstant.Status.hidden.toString());
		updateOneByObj(folder);
	}

	public int deleteOneByKey(long folderid) {
		Folder folder = folderM.selectByPrimaryKey(folderid);
		// 无此项直接跳出
		if (folder == null) {
			return 0;
		}
		// 先到父节点处减少子节点数量
		if (folder.getParentid() != 0) {
			Folder fp = folderM.selectByPrimaryKey(folder.getParentid());
			fp.setChildrencount(fp.getChildrencount() - 1);
			folderM.updateByPrimaryKeySelective(fp);
		}
		// 接着删除相关孩子节点
		Folder f = new Folder();
		f.setParentid(folderid);
		List<FolderVo> children = folderM.selectBySelective(f, 0, 0);
		for (FolderVo fv : children) {
			// 再删除相关文章
			Article art = new Article();
			art.setFolderid(fv.getFolderid());
			List<ArticleVo> art_list = articleM.selectBySelective(art, Long.valueOf(0), Long.valueOf(0));
			for (ArticleVo a : art_list) {
				articleM.deleteByPrimaryKey(a.getArticleid());
			}
			deleteOneByKey(fv.getFolderid());
		}
		return folderM.deleteByPrimaryKey(folderid);
	}

	public int updateOneByObj(Folder folder) {
		return folderM.updateByPrimaryKeySelective(folder);
	}

	public int getListCountByObj(Folder folder) {
		return folderM.selectBySelectiveCount(folder);
	}

	public List<FolderVo> getListByObj(Folder folder, long offset, long rows) {
		return folderM.selectBySelective(folder, offset, rows);
	}

	public List<FolderVo> getListOfObjTree(Folder folder) {
		List<FolderVo> list = folderM.selectBySelective(folder, 0, 0);
		for (FolderVo fv : list) {
			fv.setChildren(getChildren(fv.getFolderid()));
		}
		return list;
	}

	public List<FolderVo> getChildren(long parentid) {
		if (parentid == 0)
			return null;
		long offset, rows;
		offset = rows = 0;
		Folder folder = new Folder();
		folder.setParentid(parentid);
		List<FolderVo> list = folderM.selectBySelective(folder, offset, rows);
		for (FolderVo fv : list) {
			fv.setChildren(getChildren(fv.getFolderid()));
		}
		return list;
	}
}
